package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wq.mapper.WqGameHomeMapper;
import com.ruoyi.wq.mapper.WqUserGameMapper;
import com.ruoyi.wq.domain.WqUserGame;
import com.ruoyi.wq.domain.vo.WqGameHomeVO;
import com.ruoyi.wq.domain.vo.WqUserGameVO;
import com.ruoyi.wq.service.IWqUserGameService;
import cn.hutool.core.util.StrUtil;

/**
 * 我的组队Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-27
 */
@Service
public class WqUserGameServiceImpl extends ServiceImpl<WqUserGameMapper, WqUserGame> implements IWqUserGameService {

    @Autowired
    private WqUserGameMapper wqUserGameMapper;
    
    /**
     * 查询我的组队
     * 
     * @param id 我的组队主键
     * @return 我的组队
     */
    @Override
    public WqUserGame selectWqUserGameById(String id)
    {
        return wqUserGameMapper.selectWqUserGameById(id);
    }

    /**
     * 查询我的组队列表
     * 
     * @param wqUserGame 我的组队
     * @return 我的组队
     */
    @Override
    public List<WqUserGame> selectWqUserGameList(WqUserGame wqUserGame)
    {
        return wqUserGameMapper.selectWqUserGameList(wqUserGame);
    }

    /**
     * 新增我的组队
     * 
     * @param wqUserGame 我的组队
     * @return 结果
     */
    @Override
    public int insertWqUserGame(WqUserGame wqUserGame)
    {
        wqUserGame.setCreateTime(DateUtils.getNowDate());
        return wqUserGameMapper.insertWqUserGame(wqUserGame);
    }

    /**
     * 修改我的组队
     * 
     * @param wqUserGame 我的组队
     * @return 结果
     */
    @Override
    public int updateWqUserGame(WqUserGame wqUserGame)
    {
        wqUserGame.setUpdateTime(DateUtils.getNowDate());
        return wqUserGameMapper.updateWqUserGame(wqUserGame);
    }

    /**
     * 批量删除我的组队
     * 
     * @param ids 需要删除的我的组队主键
     * @return 结果
     */
    @Override
    public int deleteWqUserGameByIds(String[] ids)
    {
        return wqUserGameMapper.deleteWqUserGameByIds(ids);
    }

    /**
     * 删除我的组队信息
     * 
     * @param id 我的组队主键
     * @return 结果
     */
    @Override
    public int deleteWqUserGameById(String id)
    {
        return wqUserGameMapper.deleteWqUserGameById(id);
    }

	@Override
	public TableDataInfo getPageList(WqUserGameVO wqUserGame) {
		wqUserGame.setUserId(SecurityUtils.getUserId());
		IPage<WqGameHomeVO> page = baseMapper.selectPageList(Condition.getPage(wqUserGame), wqUserGame);
		for(WqGameHomeVO vo : page.getRecords()){
			Integer sumNum = 0;
			String[] roleLimitList = vo.getRoleLimit().split("/");
			List<String> roleList = new ArrayList<String>();
			for(String role : roleLimitList){
				roleList.add(role);
			}
			if(vo.getShangUserId() != null){
				roleList.remove("对抗路");
				sumNum ++;
			}
			if(vo.getShootUserId() != null){
				roleList.remove("发育路");
				sumNum ++;
			}
			if(vo.getDayeUserId() != null){
				roleList.remove("打野");
				sumNum ++;
			}
			if(vo.getFuzhuUserId() != null){
				roleList.remove("游走");
				sumNum ++;
			}
			if(vo.getZhongUserId() != null){
				roleList.remove("中路");
				sumNum ++;
			}
			vo.setRoleLimit(StrUtil.join("/", roleList));
			vo.setSumNum(sumNum);
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public void checkUserGame(WqUserGame wqUserGame) {
		QueryWrapper<WqUserGame> queryWrapper = new QueryWrapper<>();
		WqUserGame entity = new WqUserGame();
		entity.setGameHomeId(wqUserGame.getGameHomeId());
		entity.setGameName(wqUserGame.getGameName());
		queryWrapper.setEntity(entity);
		Integer count = baseMapper.selectCount(queryWrapper);
		if(count > 0){
			throw new ServiceException("已在队伍");
		}
	}
}
